<?php
/**
 * Menus controller. Siasplays site's menus
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Content Module
 * @subpackage Controller
 * @license  New BSD License
 *
 */
class content_MenusController extends Bel_Controller {
	
	/**
	 * Menus model object
	 *
	 * @var Models_Content_Menus
	 */
	private $_menu;
	
	/**
	 * Initialization
	 *
	 */
	public function init() {
		$this->_menu = new Models_Content_Menus ( );
		parent::init ();
	}
	
	/**
	 * Displays site's top menu
	 *
	 */
	public function topAction() {
		
		$oAuth = Models_Usermanagement_Auth::getInstance();
		if($oAuth->isLoggedIn()){
			$menuName = 'top_menu_logged_in';
		}else{
			$menuName = 'top_menu';
		}
		
		if (! ($menu = $this->_cache->load ( 'menu_'.$menuName ))) {
			$menu = $this->_menu->getMenuBySystemName ( $menuName );
			$this->_cache->save ( $menu );
		}
		
				
		if (count ( $menu )) {
			
			foreach ( $menu [0] ['pages'] as &$item ) {
				
		
				if ($item ['page_url']) {
					if ($this->_request->getRequestUri () == $item ['page_url']) {
						$item ['current'] = 1;
					}
				} else {
					$page_url = '/' . $item ['page_system_name'] . '.html';
					if ($this->_request->getRequestUri () == $page_url) {
						$item ['current'] = 1;
					}
				}
			}
		} else {
			$menu [0] = array ( );
		}
		
		$this->view->assign ( 'top_menu', $menu [0] );
		$this->view->display ( 'content/menus/top.tpl' );
	}
	
	/**
	 * Displays site's bottom menu
	 *
	 */
	public function bottomAction() {

		$oAuth = Models_Usermanagement_Auth::getInstance();
		if($oAuth->isLoggedIn()){
			$menuName = 'bottom_menu_logged_in';
		}else{
			$menuName = 'bottom_menu';
		}
		
		if (! ($menu = $this->_cache->load ( 'menu_'.$menuName ))) {
			$menu = $this->_menu->getMenuBySystemName ( $menuName );
			$this->_cache->save ( $menu );
		}
		

		if (count ( $menu )) {
			foreach ( $menu [0] ['pages'] as &$item ) {
				if ($item ['page_url']) {
					if ($this->_request->getRequestUri () == $item ['page_url']) {
						$item ['current'] = 1;
					}
				} else {
					$page_url = '/content/' . $item ['page_system_name'] . '.html';
					if ($this->_request->getRequestUri () == $page_url) {
						$item ['current'] = 1;
					}
				}
			}
		} else {
			$menu [0] = array ( );
		}
		
		$this->view->assign ( 'bottom_menu', $menu [0] );
		$this->view->display ( 'content/menus/bottom.tpl' );
	}
}